/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.honor;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.api.settings.ConvertAsProperties;
//import org.openide.awt.ActionID;
//import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.util.AsyncGUIJob;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import pt.iasd.youthshelter.datamodel.Honor;
import pt.iasd.youthshelter.datamodel.service.HonorController;
import pt.iasd.youthshelter.honor.nodes.HonorChildren;
import pt.iasd.youthshelter.honor.nodes.RootNode;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//pt.iasd.youthshelter.honor//HonorList//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "HonorListTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
//@ActionID(category = "Window", id = "pt.iasd.youthshelter.honor.HonorListTopComponent")
//@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_HonorListAction",
        preferredID = "HonorListTopComponent")
@Messages({
    "CTL_HonorListAction=Honors"/*,
    "CTL_HonorListTopComponent=Honors",
    "HINT_HonorListTopComponent=List of registered honors."*/
})
public final class HonorListTopComponent extends TopComponent implements ExplorerManager.Provider {
    private static ExplorerManager explorerManager = new ExplorerManager();

    public HonorListTopComponent() {
        initComponents();
        Utilities.attachInitJob(HonorListView, loadHonorListData());
        setName(NbBundle.getMessage(HonorListTopComponent.class, "HonorListTopComponent.Title"));
        setToolTipText(NbBundle.getMessage(HonorListTopComponent.class, "HonorListTopComponent.Hint"));
        putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        HonorListScrollPane = new javax.swing.JScrollPane();
        HonorListView = new org.openide.explorer.view.ListView();

        HonorListScrollPane.setViewportView(HonorListView);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(HonorListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(HonorListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane HonorListScrollPane;
    private org.openide.explorer.view.ListView HonorListView;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // read your settings according to their version
    }
    
    public static AsyncGUIJob loadHonorListData() {
        return new AsyncGUIJob() {
            List<Honor> honors = null;
            ProgressHandle pHandle = ProgressHandleFactory.createHandle(
                    NbBundle.getMessage(HonorListTopComponent.class, "ProgressBarLoad"));

            @Override
            public void construct() {
                pHandle.start();
                HonorController controller = Lookup.getDefault().lookup(HonorController.class);
                if(controller != null) {
                    try {
                        honors = ListSorter.sortHonorsByName(controller.findHonorEntities());
                    } catch(Exception ex) {
                        Logger.getLogger(this.getClass().getName()).log(
                                Level.SEVERE, ex.getLocalizedMessage(), ex);
                        pHandle.finish();
                    }
                }
            }

            @Override
            public void finished() {
                explorerManager.setRootContext(new RootNode(new HonorChildren(honors)));
                pHandle.finish();
            }
        };
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }
}
